﻿/*----------------------------------------------------------------
    // Copyright (C) 2009 江苏华招网
    // 版权所有。 
    // 作者：吕海乐 
    //
    // 文件名：UserLogin.cs
    // 文件功能描述：用户登录,获取用户有效信息
    // 创建标识：吕海乐2009-03-12
    //
    // 修改标识：
    // 修改描述：
    //
    // 修改标识：
    // 修改描述：
    //
    // 审核人： 
// 审核时间：
// 审核说明：
 //----------------------------------------------------------------*/
using System;
using System.Collections;
using System.Configuration;
using System.Data;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections.Generic;

using HZ.DrugTrade.Web;
using HZ.DrugTrade.Model;

using HZ.DrugTrade.IBLL;
using HZ.DrugTrade.BLLFactory;

namespace HZ.DrugTrade.Web.Admin
{
    public partial class UserLogin : System.Web.UI.Page
    {
        //本系统可以登陆的角色 03 中心机构
       // private const string RoleId = "01";
        private const string RoleId = "01";//企业

        protected void Page_Load(object sender, EventArgs e)
        {
            //版本号
            ltrVersion.Text = Constants.Version;

            //是否使用验证码
            if (!Constants.IsCheckValidateCode.Equals("1"))
            {
                trCode.Visible = RequiredFieldValidator1.Enabled = false;
            }
            else
            {
                trCode.Visible = RequiredFieldValidator1.Enabled = true;
            }
        }

        /// <summary>
        /// 清空信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnClear_Click(object sender, EventArgs e)
        {
            txtUserId.Text = "";
            txtUserPwd.Text = "";
        }

        /// <summary>
        /// 用户登录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnLogin_Click(object sender, EventArgs e)
        {
            string flag = "";
            string userId = txtUserId.Text.Trim();
            string pwd = txtUserPwd.Text.Trim();
            string code = this.txtCode.Text;
            if (CurrentAccount.Account != null)
            {
                HZ.Web.WebUtil.Alert("请不要在同一个浏览器中打开多个窗口！", this);
                return;
            }
            //判断登录有效性
            IBLL_Login il = Factory.BLL_Login();
            
             if ((CurrentAccount.VerifyValidateCode(code) && Constants.IsCheckValidateCode.Equals("1"))||(!Constants.IsCheckValidateCode.Equals("1")))
            {
               

                User user = new User();
                flag = il.Login(userId , pwd , out user).ToString();
                switch (flag)
                {
                    case "0":
                        HZ.Web.WebUtil.Alert("登录失败！请检查网络。", this);
                        break;
                    case "1":
                        //获取用户信息,存入session中                   
                        CurrentAccount.SetUser(user);
                        CurrentAccount.Account = new User() { UserRole=user.UserRole, IsAdmin=user.IsAdmin,IsUserAdmin=user.IsUserAdmin,IsUsing=user.IsUsing, Org=user.Org, OperatingRuleList = user.OperatingRuleList, UserID = user.UserID, UserName = user.UserName, UserPassword = pwd, LastLoginTime = DateTime.Now, IsOnline = true };
                        string str = "parent.location='Default.aspx'";
                        Response.Write(HZ.Web.WebUtil.GetScript(str));
                        break;
                    case "2":
                        HZ.Web.WebUtil.Alert("用户名密码不匹配！", this);
                        break;
                    case "3":
                        HZ.Web.WebUtil.Alert("用户未启用！", this);
                        break;
                    default:
                        break;
                }
                this.txtCode.Text = "";
            }
            else
            {
                this.txtCode.Text = "";
                HZ.Web.WebUtil.Alert("验证码不正确！", this);
                return;

            }
        }
    }
}
